﻿using LPY.LeHealth.DAL;
using LPY.LeHealth.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LPY.LeHealth.Service
{
    using AutoMapper;
    using AutoMapper.Execution;
    using AutoMapper.Configuration;
    using AutoMapper.Mappers;
    using LPY.LeHealth.DTO;
    using System.Data;
    public class PhysicalService
    {
        private PhysicalDAL dal;

        
        public PhysicalService()
        {
            dal = new PhysicalDAL();
        }

        #region 体检项目管理

        //获取列表
        public List<PhysicalItem> GetPhysicalItemList(int pageIndex, int pageSize, out long totalSize)
        {
            var dto= dal.GetPhysicalItemList(pageIndex, pageSize, out totalSize);
            return Mapper.Map<List<PhysicalItem>>(dto);
        }

        //添加
        public bool AddPhysicalItem(PhysicalItem model)
        {
            var dto= Mapper.Map<PhysicalItemDTO>(model);
            dto.CreateTime = DateTime.Now;
            var result= dal.AddPhysicalItem(dto);
            return result;
        }

        /// <summary>
        /// 根据id获取体检项目信息
        /// </summary>
        /// <param name="id">体检项目id</param>
        /// <returns>体检项目信息</returns>
        public PhysicalItem GetPhysicalItemById(int id)
        {
            var dto = dal.GetPhysicalItemById(id);
            var rtv = Mapper.Map<PhysicalItemDTO, PhysicalItem>(dto);
            return rtv;
        }

        /// <summary>
        /// 修改体检项目信息
        /// </summary>
        public bool UpdatePhysicalItem(PhysicalItem physical)
        {
            var physicaldto = Mapper.Map<PhysicalItem, PhysicalItemDTO>(physical);
            var rtv = dal.UpdatePhysicalItem(physicaldto);
            return rtv;
        }

        /// <summary>
        /// 删除体检项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeletePhysicalItemById(int id)
        {
            return dal.DeletePhysicalItemById(id);
        }

        #endregion

        /// <summary>
        /// 获取体检套餐列表（包含：单项、套餐）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public List<ComboDTO> GetComboList(int comboTypeId, int pageIndex, int pageSize, out long totalSize)
        {
            return dal.GetComboList(comboTypeId,pageIndex, pageSize,out totalSize);
        }

        /// <summary>
        /// 获取单项体检列表
        /// </summary>
        /// <returns></returns>
        public List<Combo> GetPhysicalSingle(int pageIndex,int pageSize, out long totalSize)
        {
            var dto= dal.GetComboList(true,pageIndex, pageSize, out totalSize);
            return Mapper.Map<List<Combo>>(dto);
        }

        public List<ComboDTO> GetPhysicalSingleDTO(int pageIndex, int pageSize, out long totalSize)
        {
            var dto = dal.GetComboList(true, pageIndex, pageSize, out totalSize);
            return dto;
        }

        /// <summary>
        /// 获取体检套餐列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public List<Combo> GetPhysicalCombo(int pageIndex, int pageSize, out long totalSize)
        {
            var dto = dal.GetComboList(false, pageIndex, pageSize, out totalSize);
            return Mapper.Map<List<Combo>>(dto);
        }

        public List<ComboDTO> GetPhysicalComboDTO(int pageIndex, int pageSize, out long totalSize)
        {
            var dto = dal.GetComboList(false, pageIndex, pageSize, out totalSize);
            return dto;
        }

        /// <summary>
        /// 获取所有体检类型
        /// </summary>
        /// <returns></returns>
        public List<ComboType> GetAllComboType()
        {
            var dto = dal.GetAllComboType();
            return Mapper.Map<List<ComboType>>(dto);
        }

        /// <summary>
        /// 获取所有体检项列表
        /// </summary>
        public List<PhysicalItem> GetAllPhysicalItem()
        {
            List<PhysicalItemDTO> dto = dal.GetAllPhysicalItem();
            return Mapper.Map<List<PhysicalItem>>(dto);
        }

        /// <summary>
        /// 根据医院Id获取体检列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<PhysicalItem> GetPhysicalsByHospitalId(int id)
        {
            List<PhysicalItemDTO> dto = dal.GetPhysicalsByHospitalId(id);
            return Mapper.Map<List<PhysicalItem>>(dto);
        }

        public bool DeletePhysicalById(int id)
        {
            var rtv=dal.DeleteComboById(id);
            return rtv;
        }

        //添加单项体检套餐
        public bool AddComboSingle(string name, int hospital, int comboTypeId, double price, string appropriate, string details, int item)
        {
            var dto = new ComboDTO();
            dto.Name = name;
            dto.Price = (decimal)price;
            dto.Single = true;
            dto.HospitalId = hospital;
            dto.Details = details;
            dto.Appropriate = appropriate;
            dto.ComboTypeId = comboTypeId;
            dto.CreateTime = DateTime.Now;

            List<int> items = new List<int>();
            items.Add(item);

            bool result= dal.AddCombo(dto,items);
            return result;
        }

        //添加多项体检套餐
        public bool AddCombo(string name, int hospital, int comboTypeId, double price, string appropriate, string details, List<int> items)
        {
            var dto = new ComboDTO();
            dto.Name = name;
            dto.Price = (decimal)price;
            dto.Single = false;
            dto.HospitalId = hospital;
            dto.Details = details;
            dto.Appropriate = appropriate;
            dto.ComboTypeId = comboTypeId;
            dto.CreateTime = DateTime.Now;

            bool result = dal.AddCombo(dto, items);
            return result;
        }

        //编辑单项体检套餐
        public bool EditCombo(int id, string name, int hospital, int comboTypeId, double price, string appropriate, string details, List<int> items)
        {
            if (id <= 0)
                return false;

            var dto = new ComboDTO();
            dto.Id = id;
            dto.Name = name;
            dto.Price = (decimal)price;
            //dto.Single = true;
            dto.HospitalId = hospital;
            dto.Details = details;
            dto.Appropriate = appropriate;
            dto.ComboTypeId = comboTypeId;
            //dto.CreateTime = DateTime.Now;

            //List<int> items = new List<int>();
            //items.Add(item);

            bool result = dal.EditCombo(dto, items);
            return result;
        }
        /// <summary>
        /// 根据套餐获取体检项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Combo GetCombo(int id)
        {
            ComboDTO dto = dal.GetCombo(id);
            var combo= Mapper.Map<Combo>(dto);
            List<ComboToPhysicalItemDTO> items = dal.GetComboToPhysicalItems(dto.Id);// dal.GetPhysicalsByHospitalId(dto.Id);
            List<int> itemsId=new List<int>();
            items.ForEach(m => itemsId.Add(m.PhysicalItemId));
            var pis= dal.GetPhysicalItems(itemsId);
            combo.Items = Mapper.Map<List<PhysicalItem>>(pis);
            return combo;
        }


        /// <summary>
       /// 根据ID获取体检项目
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public List<PhysicalItemDTO> GetcomTophy(int id)
        {
            ComTophyDAL dal = new ComTophyDAL();
            return dal.GetcomTophy(id);
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="fiels"></param>
        /// <returns></returns>
        public bool AddPhysicitemFile(PhysicalItemFiles fiels)
        {
            return dal.AddPhysicitemFile(fiels);
        }
        /// <summary>
        /// 显示所有文件
        /// </summary>
        /// <returns></returns>
         public List<PhysicalItemFiles>ShowFile(string id)
        {
            return dal.ShowFile(id);
        }
        /// <summary>
        /// 批量导入体检项目信息
        /// </summary>
        /// <param name="phyname"></param>
        /// <param name="porpuse"></param>
        /// <param name="price"></param>
        /// <param name="hospitalid"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public bool InsertPhysical(string phyname,string porpuse,float price,int hospitalid,int typeid)
         {
             return dal.InsertPhysical(phyname, porpuse, price, hospitalid, typeid);
         }
        /// <summary>
        /// 导入数据成功后删除文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
          public bool DeletePhysical(int id)
        {
            return dal.DeletePhysical(id);
        }

         /// <summary>
        /// 批量添加套餐项目
        /// </summary>
        /// <param name="comb"></param>
        /// <returns></returns>
        public bool AddComboItem(Combo comb,decimal OriginalPrice)
          {
              return dal.AddComboItem(comb,OriginalPrice);
          }

         /// <summary>
        /// 根据项目类型名称查找对应的ID
        /// </summary>
        /// <returns></returns>
        public int ShowTypeid(string typename)
        {
            return dal.ShowTypeid(typename);
        }
          /// <summary>
        /// 根据套餐类型名称查找对应的类型ID
        /// </summary>
        /// <param name="comname"></param>
        /// <returns></returns>
        public int Comtypid(string comname)
        {
            return dal.Comtypid(comname);
        }

        /// <summary>
        /// 根据医院名称查找对应的医院ID
        /// </summary>
        /// <returns></returns>
        public int ShowHospitalId(string hospitalname)
        {
            return dal.ShowHospitalId(hospitalname);
        }

         /// <summary>
        /// 查找套餐表中当前插入行的ID以及体检项目
        /// </summary>
        /// <returns></returns>
        public List<string> ShowComtable()
        {
            return dal.ShowComtable();
        }

          /// <summary>
        /// 根据套餐ID查找套餐及项目关系表中是否存在此套餐信息
        /// </summary>
        /// <param name="combid"></param>
        /// <returns></returns>
        public DataTable ShowComPrhyItem(int combid)
        {
            return dal.ShowComPrhyItem(combid);
        }

          /// <summary>
        /// 查找体检项目对应的ID
        /// </summary>
        /// <param name="dtoname"></param>
        /// <returns></returns>
        public int Showphysicalitemdto(string dtoname)
        {
            return dal.Showphysicalitemdto(dtoname);
        }

         /// <summary>
        /// 循环添加t_combotophysicalitem表
        /// </summary>
        /// <param name="combid"></param>
        /// <param name="PhysicalItemId"></param>
        /// <returns></returns>
        public bool Addt_combotophysicalitem(int combid,int PhysicalItemId)
        {
            return dal.Addt_combotophysicalitem(combid, PhysicalItemId);
        }
    }
}
